package com.ztom.daily.n2022.m3;

import java.util.ArrayList;
import java.util.List;

/**
 * 自除数
 * <p>
 * https://leetcode-cn.com/problems/self-dividing-numbers/
 *
 * @author ZhangTao
 */
public class Day31SelfDividingNumbers {

    public List<Integer> selfDividingNumbers(int left, int right) {
        List<Integer> res = new ArrayList<>();
        if (left > right) {
            return res;
        }

        for (int i = left; i <= right; i++) {
            if (isSelfDividing(i)) {
                res.add(i);
            }
        }
        return res;
    }

    private boolean isSelfDividing(int num) {
        int tmp = num;
        while (tmp > 0) {
            // 取个位数
            int d = tmp % 10;
            if (d == 0 || num % d != 0) {
                return false;
            }
            tmp /= 10;
        }
        return true;
    }
}
